// pages/xgphone/xgphone.js
import store from "../../store/index";
import create from "../../utils1/create";

create.Page(store, {
  use: ['isToken', "infouser", "globhttp"],

  /**
   * 页面的初始数据
   */
  data: {
    isHidden: true,
    OldUserPass: "",
    NewUserPass: ""
  },

  xgpass: function () {
    if (this.data.OldUserPass ==="") {
      wx.showToast({
        icon: "none",
        title: '请输入旧密码',
        duration: 2000
      });
    } else if (this.data.NewUserPass === "") {
      wx.showToast({
        icon: "none",
        title: '请输入新密码',
        duration: 2000
      });
    } else {
      console.log("infouser", store.data.infouser.Pson_Name);
      console.log("infouser", store.data.infouser);
      wx.request({
        url: this.store.data.globhttp + "PsonUserChgPass",
        method: 'POST',
        header: {
          'content-type': 'application/json',
          "content-type": "application/x-www-form-urlencoded"
        },
        data: JSON.stringify({
          User_Id: store.data.infouser.User_Id,
          Token_Code: store.data.isToken,
          OldUserPass: this.data.OldUserPass,
          NewUserPass: this.data.NewUserPass,
        }),
        success: (res) => {
          console.log("res", res);
          if (res.data.RltCode === 13) {
            this.store.setToken("setpass");
          } else if (res.data.RltCode === 1) {
            setTimeout(function() {
              wx.showToast({
                icon: "none",
                title: '修改密码成功',
                duration: 1000
              });
            },2000)
            wx.showToast({ icon: 'loading', title: '', mask: true });
            store.data.xgUserPass = {
              OldUserPass: this.data.OldUserPass,
              NewUserPass: this.data.NewUserPass
            };
            setTimeout(function() {
              wx.switchTab({
                url: '/pages/mine/mine'
              });
             },3000)
          } else if (res.data.RltCode === 0) {
            wx.showToast({
              icon: "none",
              title: '修改密码失败',
              duration: 2000
            });
          } else {
            console.log(res.data.RltMsg);
            wx.showToast({
              icon: "none",
              title: '未知错误',
              duration: 2000
            });
          }
        },
        fail: (error) => {
          console.error('请求失败:', error);
          wx.showToast({
            icon: "none",
            title: "网络请求失败",
            duration: 2000
          });
        }
      });
    }
  },

  bindKeyInput: function (e) {
    console.log("e",e);
    const { inputname } = e.currentTarget.dataset;
    this.setData({
      [inputname]: e.detail.value
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    setTimeout(() => {
      this.setData({
        isHidden: false
      });
    }, 500); // 500毫秒后显示页面
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
});